// component_selectfromlist.js

/*
FUNCTION NAME: resetItems
-----------------------------------------------------------------------
The following deselects all items in the specified select box.

Variable Definitions
====================
from		= The List from which selected items will be deselected
*/
function resetItems(from) {
	var i;
	for (i=0; i<from.options.length; i++)
	{
		from.options[i].selected = false;
	}
}

/*
FUNCTION NAME: selectAllItems
-----------------------------------------------------------------------
The following selects all items in the specified select box.

Variable Definitions
====================
from		= The List from which all items will be selected

Always have the list set as multiple so that we can set as selected all
the options in the list. This way, all the options are sent to the 
server.  

*/
function selectAllItems(from) {
    if( from.disabled == true ){
       return;
    }
    
    if( from.multiple != true){    
    	from.multiple = true;
    }
    from.focus();
	var i;
	for (i=0; i<from.options.length; i++)
	{
		from.options[i].selected = true;

	}
}

/*
FUNCTION NAME: sortSelectBox
-----------------------------------------------------------------------
The following function sorts the specified select box in accending order.

Variable Definitions
====================
list		 = The List to be sorted
thisButton	 = The button pressed by the user
sortFunction = The Sort Order Function to be used
			   none     = No Sorting
			   asc      = Ascending
			   desc     = Decending
			   desc-cio = Ascending (Case Insensitive)
			   desc-cio = Descending (Case Insensitive)
			   Any other values will be treated as a custom function name.
*/
function sortSelectBox(list, thisButton, sortFunction) {
	if (list.options==null) { 
			return; 
	}

	thisButton.focus();	// workaround to fix the JS error "Object doesn't support this property or method"
		
	var tempList = new Array();
	for (var i=0; i<list.options.length; i++) {
		tempList[tempList.length] = new Option( list.options[i].text, list.options[i].value, list.options[i].defaultSelected, list.options[i].selected) ;
	}

	if (tempList.length==0) { 
		return; 
	}
	
	if (sortFunction == 'none' || sortFunction == 'None' || sortFunction == 'NONE') {
		return;
	} else if (sortFunction == 'asc' || sortFunction == 'ASC') {
		sortFunction = sortSelectBoxAccendingCaseSensitive;
	} else if (sortFunction == 'desc' || sortFunction == 'DESC') {
		sortFunction = sortSelectBoxDecendingCaseSensitive;
	} else if (sortFunction == 'asc-cio' || sortFunction == 'ASC-CIO') {
		sortFunction = sortSelectBoxAccendingCaseInsensitive;
	} else if (sortFunction == 'desc-cio' || sortFunction == 'DESC-CIO') {
		sortFunction = sortSelectBoxDecendingCaseInsensitive;
	}
	var sortCommand = sortFunction;
	sortCommand(tempList)
	
	for (var i=0; i<tempList.length; i++) {
		list.options[i] = new Option(tempList[i].text, tempList[i].value, tempList[i].defaultSelected, tempList[i].selected);
	}
}

/*
FUNCTION NAME: sortSelectBoxAccendingCaseSensitive
-----------------------------------------------------------------------
The following function sorts the specified select box in accending order.

Variable Definitions
====================
tempList		= The List to be sorted
*/
function sortSelectBoxAccendingCaseSensitive(tempList) {
	tempList = tempList.sort( 
				function(a,b) { 
					if ((a.text+"") < (b.text+"")) { return -1; }
					if ((a.text+"") > (b.text+"")) { return 1; }
					return 0;
				}	 
			);
}

/*
FUNCTION NAME: sortSelectBoxAccendingCaseInsensitive
-----------------------------------------------------------------------
The following function sorts the specified select box in accending order.

Variable Definitions
====================
tempList		= The List to be sorted
*/
function sortSelectBoxAccendingCaseInsensitive(tempList) {
	tempList = tempList.sort( 
			function(a,b) { 
				if ((a.text.toLowerCase()+"") < (b.text.toLowerCase()+"")) { return -1; }
				if ((a.text.toLowerCase()+"") > (b.text.toLowerCase()+"")) { return 1; }
				return 0;
			}	 
		);
}

/*
FUNCTION NAME: sortSelectBoxDecendingCaseSensitive
-----------------------------------------------------------------------
The following function sorts the specified select box in accending order.

Variable Definitions
====================
tempList		= The List to be sorted
*/
function sortSelectBoxDecendingCaseSensitive(tempList) {
	tempList = tempList.sort( 
			function(a,b) { 
				if ((a.text+"") > (b.text+"")) { return -1; }
				if ((a.text+"") < (b.text+"")) { return 1; }
				return 0;
			}	 
		);
}

/*
FUNCTION NAME: sortSelectBoxDecendingCaseInsensitive
-----------------------------------------------------------------------
The following function sorts the specified select box in accending order.

Variable Definitions
====================
tempList		= The List to be sorted
*/
function sortSelectBoxDecendingCaseInsensitive(tempList) {
	tempList = tempList.sort( 
			function(a,b) { 
				if ((a.text.toLowerCase()+"") > (b.text.toLowerCase()+"")) { return -1; }
				if ((a.text.toLowerCase()+"") < (b.text.toLowerCase()+"")) { return 1; }
				return 0;
			}	 
		);
}

/*
FUNCTION NAME: getSelectedItems
-----------------------------------------------------------------------
The following function retrieves those items selected by the user in the specified select box.

Variable Definitions
====================
from		= The List from which selected items will be retrieved
*/
function getSelectedItems(from) {
	var i;
	var selected = new Array();
	for (i=0; i<from.options.length; i++)
	{
		if (from.options[i].selected)
			selected[selected.length] = from.options[i];
	}
	return selected;
}

/*
FUNCTION NAME: getAllItems
-----------------------------------------------------------------------
The following function retrieves all items in the specified select box.

Variable Definitions
====================
from		= The List from which all items will be retrieved
*/
function getAllItems(from) {
	var i;
	var selected = new Array();
	for (i=0; i<from.options.length; i++)
	{
			selected[selected.length] = from.options[i];
	}
	return selected;
}

/*
FUNCTION NAME: moveSelectedItems
-----------------------------------------------------------------------
The following function moves those items selected by the user from one select box to another.

Variable Definitions
====================
from		= The List from which selected items will be moved
to			= The List to which selected items will be moved
thisButton	= The button pressed by the user
sortFunction = The Sort Order Function to be used
			   none     = No Sorting
			   asc      = Ascending
			   desc     = Decending
			   desc-cio = Ascending (Case Insensitive)
			   desc-cio = Descending (Case Insensitive)
			   Any other values will be treated as a custom function name.
*/
function moveSelectedItems(from,to,thisButton,sortFunction) {
	resetItems(to);
	var items = getSelectedItems(from);
	if (items.length==0)
		return;
	
	var i;
	for (i=0; i<items.length; i++)
		to.appendChild(items[i]);

	sortSelectBox(to, thisButton, sortFunction);
}

/*
FUNCTION NAME: moveAllItems
-----------------------------------------------------------------------
The following function moves all items from one select box to another.

Variable Definitions
====================
from		= The List from which all items will be moved
to			= The List to which all items will be moved
thisButton	= The button pressed by the user
sortFunction = The Sort Order Function to be used
			   none     = No Sorting
			   asc      = Ascending
			   desc     = Decending
			   desc-cio = Ascending (Case Insensitive)
			   desc-cio = Descending (Case Insensitive)
			   Any other values will be treated as a custom function name.
*/
function moveAllItems(from,to,thisButton,sortFunction) {
	var items = getAllItems(from);
	if (items.length==0)
		return;
	
	var i;
	for (i=0; i<items.length; i++)
		to.appendChild(items[i]);

	sortSelectBox(to, thisButton, sortFunction);
	resetItems(to);
}

/*
FUNCTION NAME: getAllSelectfromlists
-----------------------------------------------------------------------
The following indexes all "selectFromList" components on the page.

Variable Definitions
====================
none
*/
function getAllSelectfromlists() {
  var theClass = 'selectfromlist';
  var gotCollection = new Array();
  var gotAll = new Array();
  gotAll = document.getElementsByTagName("TABLE");
  j=0;
   for (i=0; i<gotAll.length; i++) {
	   if ((gotAll[i].className==theClass)||(gotAll[i].className.search(" " + theClass)!=-1)||(gotAll[i].className.search(" " + theClass + " ")!=-1)||(gotAll[i].className.search(theClass + " ")!=-1)) {
			gotCollection[j]=gotAll[i];
			j++;
	   }
	}
    return gotCollection;
}

/*
FUNCTION NAME: preprocessAllSelectfromlists
-----------------------------------------------------------------------
The following selects all items under the Selected heading for all "selectFromList" components on the page.

Variable Definitions
====================
none
*/
function preprocessAllSelectfromlists() {
	var componentCollection = getAllSelectfromlists();
	for (i=0; i<componentCollection.length; i++) {
		resetItems(componentCollection[i].getElementsByTagName('select')[0])
		selectAllItems(componentCollection[i].getElementsByTagName('select')[1]);
	}
}

